﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrewTurn.Wiki.PluginFramework;
using ScrewTurn.Wiki;

namespace IdeaNMR.Web
{
    public static class ScrewTurnWikiFacade
    {
        public static string Username
        {
            get { return ScrewTurn.Wiki.SessionFacade.CurrentUsername; }
        }

        public static string MainUrl
        {
            get { return ScrewTurn.Wiki.Settings.MainUrl; }
        }

        public static string DateTimeFormat
        {
            get { return ScrewTurn.Wiki.Settings.DateTimeFormat; }
        }

        public static string PublicDirectory
        {
            get { return ScrewTurn.Wiki.Settings.PublicDirectory; }
        }

        public static void LogEntry(string msg, EntryType entryType, string username)
        {
            ScrewTurn.Wiki.Log.LogEntry(msg, entryType, username);
        }

        public static bool CreatePage(string pageName, string pageTitle, string pageContent, string[] cats)
        {
            bool result;
            ScrewTurn.Wiki.PluginFramework.IPagesStorageProviderV30 provider = ScrewTurn.Wiki.Collectors.PagesProviderCollector.GetProvider(ScrewTurn.Wiki.Settings.DefaultPagesProvider);

            // Create Categories List
            List<CategoryInfo> categories = new List<CategoryInfo>();
            for (int i = 0; i < cats.Length; i++)
            {
                string cCategory = cats[i];
                CategoryInfo cI = Pages.FindCategory(cCategory);
                if (cI == null)
                {
                    Pages.CreateCategory((string)null, cCategory, provider);
                    cI = Pages.FindCategory(cCategory);
                }
                if (cI.Provider == provider) categories.Add(cI);
            }

            string fullName = ScrewTurn.Wiki.PluginFramework.NameTools.GetFullName(null, pageName);
            ScrewTurn.Wiki.PluginFramework.PageInfo pg = ScrewTurn.Wiki.Pages.FindPage(fullName, provider);
            if (pg == null)
            {
                ScrewTurn.Wiki.Pages.CreatePage((string)null, pageName, provider);
                pg = ScrewTurn.Wiki.Pages.FindPage(fullName, provider);
            }

            result = ScrewTurn.Wiki.Pages.ModifyPage(pg, pageTitle, Username, DateTime.Now, "Page is created by publishing corresponding component.", pageContent, null, null, ScrewTurn.Wiki.PluginFramework.SaveMode.Normal);

            Pages.Rebind(pg, categories.ToArray());

            return result;
        }

        public static string GetPageContent(string pageName)
        {
            ScrewTurn.Wiki.PluginFramework.IPagesStorageProviderV30 provider = ScrewTurn.Wiki.Collectors.PagesProviderCollector.GetProvider(ScrewTurn.Wiki.Settings.DefaultPagesProvider);

            string fullName = ScrewTurn.Wiki.PluginFramework.NameTools.GetFullName(null, pageName);
            ScrewTurn.Wiki.PluginFramework.PageInfo pg = ScrewTurn.Wiki.Pages.FindPage(fullName, provider);
            if (pg == null) return null;

            return pg.Provider.GetContent(pg).Content;
        }

        public static DateTime ToUsersLocalTime(this DateTime dt)
        {
            return ScrewTurn.Wiki.Preferences.AlignWithTimezone(dt);
        }
    }
}